package com.frxuexi.park2305.my

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.SimpleAdapter.ViewBinder
import androidx.activity.viewModels
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.SPUtils
import com.frxuexi.lib_base.entity.MyApplyBean
import com.frxuexi.lib_base.entity.ParkBeanItem
import com.frxuexi.lib_base.entity.RepairBeanItem
import com.frxuexi.lib_base.entity.TaskBeanItem
import com.frxuexi.lib_base.entity.VisitBeanItem
import com.frxuexi.lib_base.http.Const
import com.frxuexi.lib_base.http.RequestType
import com.frxuexi.lib_base.mvi.BaseActivity
import com.frxuexi.lib_base.mvi.UiState
import com.frxuexi.park2305.R
import com.frxuexi.park2305.databinding.ActivityMyApplyBinding
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch

@Route(path = "/app/MyApplyActivity")
@AndroidEntryPoint
class MyApplyActivity : BaseActivity<ActivityMyApplyBinding,MyIntent,MyViewModel>() {
    override val binding: ActivityMyApplyBinding by lazy { ActivityMyApplyBinding.inflate(layoutInflater) }
    override val vm: MyViewModel  by viewModels<MyViewModel>()
    val list= mutableListOf<MyApplyBean>()
    val myApplyAdapter by lazy { MyApplyAdapter() }
    override fun initData() {
        lifecycleScope.launch {
            vm.uiState.collect{
                when(it){
                    is UiState.onSuccess<*> ->{
                        when(it.type){
                            RequestType.DEFAULT->{
                                val items = it.data as Collection<ParkBeanItem>
                                items.filter { it.userId==SPUtils.getInstance().getInt(Const.UID) }.forEach {
                                    list.add(MyApplyBean("车位申请",it.spaceStatus,"申请${it.areaName} ${it.spaceNumber}车位","2025-09-10 10:00"))
                                }
                            }
                            RequestType.SELECT_VISIT->{
                                val items = it.data as Collection<VisitBeanItem>
                                items.forEach {
                                    list.add(MyApplyBean("来访申请",it.visitStatus,"访客：${it.visitorName}(1人)|${it.appointmentTime.split(" ")[0]}",it.appointmentTime))
                                }
                            }
                            RequestType.SELECT_TASK->{
                                val items = it.data as Collection<TaskBeanItem>
                                items.forEach {
                                    list.add(MyApplyBean("巡更申请",it.patrolStatus,"${it.taskName}巡更申请",it.patrolTime))
                                }
                            }
                            RequestType.SELECT_REPAIR->{
                                val items = it.data as Collection<RepairBeanItem>
                                items.forEach {
                                    list.add(MyApplyBean("报修申请",it.processingStatus,"${it.faultDescription}报修",it.repairTime))
                                }
                            }

                            else -> {}

                        }
                        myApplyAdapter.apply {
                            data.clear()
                            addData(list)
                            notifyDataSetChanged()
                        }
                    }

                    else -> {}
                }
            }
        }
    }

    override fun initView() {
        binding.rvMy.apply {
            adapter=myApplyAdapter
            layoutManager=LinearLayoutManager(this@MyApplyActivity)
        }
        vm.sendIntent(MyIntent.getMyPark)
        vm.sendIntent(MyIntent.getMyVisit(SPUtils.getInstance().getString(Const.UNAME)))
        vm.sendIntent(MyIntent.getMyTask(SPUtils.getInstance().getInt(Const.UID)))
        vm.sendIntent(MyIntent.getMyRepair(SPUtils.getInstance().getString(Const.UNAME)))

        binding.myParkCon.setOnClickListener {
            myApplyAdapter.apply {
                data.clear()
                addData(list.filter { it.type=="车位申请" })
                notifyDataSetChanged()
            }
        }

        binding.myVisitCon.setOnClickListener {
            myApplyAdapter.apply {
                data.clear()
                addData(list.filter { it.type=="来访申请" })
                notifyDataSetChanged()
            }
        }

        binding.myRepairCon.setOnClickListener {
            myApplyAdapter.apply {
                data.clear()
                addData(list.filter { it.type=="报修申请" })
                notifyDataSetChanged()
            }
        }

        binding.myPatrolCon.setOnClickListener {
            myApplyAdapter.apply {
                data.clear()
                addData(list.filter { it.type=="巡更申请" })
                notifyDataSetChanged()
            }
        }
    }

//    第1套：10月3日提交完，10月4日检查
//    第2套：10月5日提交完，10月6日检查
//    第3套：10月7日提交完，10月8日检查

}